package com.aerilys.taskboard.client.views.implementation;

import java.util.ArrayList;
import java.util.List;

import com.aerilys.taskboard.client.activities.GeneralActivity;
import com.aerilys.taskboard.client.places.DetailsViewPlace;
import com.aerilys.taskboard.client.views.GeneralView;
import com.aerilys.taskboard.shared.Note;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class GeneralViewImpl extends Composite implements GeneralView
{

  private static GeneralViewImplUiBinder uiBinder = GWT.create(GeneralViewImplUiBinder.class);

  @UiField(provided = true)
  CellList<Note> listBoxTasks = new CellList<Note>(new NoteCell());

  interface GeneralViewImplUiBinder extends UiBinder<Widget, GeneralViewImpl>
  {
  }

  private Presenter presenter;

  public GeneralViewImpl()
  {
    initWidget(uiBinder.createAndBindUi(this));

    listBoxTasks.setStyleName("listBoxTasks");
    listBoxTasks.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    final SingleSelectionModel<Note> selectionModel = new SingleSelectionModel<Note>();
    listBoxTasks.setSelectionModel(selectionModel);

    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      public void onSelectionChange(SelectionChangeEvent event) {
        Note selected = selectionModel.getSelectedObject();
        if (selected != null) {
          GeneralActivity.ActualNote = selected;   
          presenter.goTo(new DetailsViewPlace("Details"));      
        }
      }
    });
    
    LoadNotes(new ArrayList<Note>());
  }

  /*@UiHandler("goodbyeLink")
  void onClick(ClickEvent e)
  {
    this.presenter.goTo(new DetailsViewPlace("Details"));
  }*/

  @Override
  public void LoadNotes(List<Note> listeNotes)
  {
    Note note = new Note();
    note.setNom("test");
    note.setDescription("Description");
    listeNotes.add(note);
    
    listBoxTasks.setRowCount(listeNotes.size());
    listBoxTasks.setRowData(0, listeNotes);
  }

  @Override
  public void setPresenter(Presenter presenter)
  {
    this.presenter = presenter;
  }

  @Override
  public Presenter getPresenter()
  {
    return this.presenter;
  }

}

class NoteCell extends AbstractCell<Note>
{
  @Override
  public void render(com.google.gwt.cell.client.Cell.Context context, Note value, SafeHtmlBuilder sb)
  {
    int maxLength = (value.getDescription().length() > 50) ? 50 : value.getDescription().length();
    sb.appendHtmlConstant("<h4>" + value.getNom() + "</h4> &nbsp;&nbsp;&nbsp;"
        + value.getDescription().substring(0, maxLength));
  }
}
